﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace TasksUI
{
    public partial class Statuses : Form
    {
        public Statuses()
        {
            InitializeComponent();
        }

        public Statuses(TasksModel.TaskLibrary library)
            : this()
        {
            this.library = library;
            lb.BeginUpdate();
            foreach (TasksModel.TaskStatus ta in library.Statuses)
                lb.Items.Add(ta);
            lb.EndUpdate();
        }

        public static void Show(TasksModel.TaskLibrary library)
        {
            using (Statuses f = new Statuses(library))
            {
                f.ShowDialog(Form.ActiveForm);
            }
        }

        private TasksModel.TaskLibrary library;

        private void lb_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (lb.SelectedItem == null)
                return;
            TasksModel.TaskStatus ta = (TasksModel.TaskStatus)lb.SelectedItem;
            tbName.Text = ta.Name;
            cbInitial.Checked = ta.IsInitialStatus;
            cbFinal.Checked = ta.IsFinalStatus;
            cbInitial.Enabled = true;
            cbFinal.Enabled = true;
            tbName.Enabled = true;
            bSave.Enabled = true;
            bRemove.Enabled = !library.CheckStatusUsed(ta);
        }

        private void bSave_Click(object sender, EventArgs e)
        {
            string name = tbName.Text;
            TasksModel.TaskStatus t = (TasksModel.TaskStatus)lb.SelectedItem;
            foreach (TasksModel.TaskStatus ta in lb.Items)
                if (ta.ID != t.ID)
                {
                    if (ta.Name == name)
                    {
                        MessageBox.Show(this, "Status name already exists");
                        return;
                    }
                    if (ta.IsFinalStatus && cbFinal.Checked)
                    {
                        MessageBox.Show(this, "Final status already exists");
                        return;
                    }
                    if (ta.IsInitialStatus && cbInitial.Checked)
                    {
                        MessageBox.Show(this, "Initial status already exists");
                        return;
                    }
                }
            tbName.Text = string.Empty;
            tbName.Enabled = false;
            cbInitial.Enabled = false;
            cbFinal.Enabled = false;
            bSave.Enabled = false;
            bRemove.Enabled = false;
            t.SuspendChangedEvent();
            try
            {
                t.IsFinalStatus = cbFinal.Checked;
                t.IsInitialStatus = cbInitial.Checked;
                t.Name = name;
            }
            finally
            {
                t.ResumeChangedEvent();
            }
            lb.Items[lb.SelectedIndex] = t;
        }

        private void bRemove_Click(object sender, EventArgs e)
        {
            TasksModel.TaskStatus t = (TasksModel.TaskStatus)lb.SelectedItem;
            if (library.CheckStatusUsed(t))
            {
                MessageBox.Show(this, "Status is being used");
                return;
            }
            library.Statuses.Remove(t);
            lb.Items.RemoveAt(lb.SelectedIndex);
            tbName.Text = string.Empty;
            tbName.Enabled = false;
            cbInitial.Enabled = false;
            cbFinal.Enabled = false;
            bSave.Enabled = false;
            bRemove.Enabled = false;
        }

        private void cbFinal_CheckedChanged(object sender, EventArgs e)
        {
            if (cbFinal.Checked)
                cbInitial.Checked = false;
        }

        private void cbInitial_CheckedChanged(object sender, EventArgs e)
        {
            if (cbInitial.Checked)
                cbFinal.Checked = false;
        }

        private void bClose_Click(object sender, EventArgs e)
        {
            bool hasInitial = false;
            bool hasFinal = false;
            foreach (TasksModel.TaskStatus ta in lb.Items)
            {
                hasInitial |= ta.IsInitialStatus;
                hasFinal |= ta.IsFinalStatus;
            }
            if (!hasInitial)
            {
                MessageBox.Show(this, "No initial status exists");
                return;
            }
            if (!hasFinal)
            {
                MessageBox.Show(this, "No final status exists");
                return;
            }
            DialogResult = System.Windows.Forms.DialogResult.OK;
        }

        private void Statuses_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)27)
                bClose_Click(null, null);
        }
    }
}